// 方法装饰器可以实现缓存结果
function cache(target:any,propertyKey:string,descriptor){
    const old = descriptor.value;
    const cachMap = new Map<string,any>();
    descriptor.value = function(...args:any[]){
        const key = JSON.stringify(args);
        if(cachMap.has(key)){
            return cachMap.get(key);
        }
        const result = old.apply(this,args);
        cachMap.set(key,result);
        return result;
    }
    return descriptor;
}
class MathOperations{
    @cache
    factorial(n:number):number{
        if(n<=1) return 1;
        else return n * this.factorial(n-1);
    }
}
const instance = new MathOperations();
console.log(instance.factorial(5));
console.log(instance.factorial(5));